1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gobject.ObjectG; 26 27 private import core.memory; 28 private import glib.ConstructionException; 29 private import glib.Str; 30 private import glib.c.functions : g_datalist_get_flags; 31 private import gobject.Binding; 32 private import gobject.Closure; 33 private import gobject.DClosure; 34 private import gobject.ObjectG; 35 private import gobject.ParamSpec; 36 private import gobject.Signals; 37 private import gobject.TypeInterface; 38 private import gobject.Value; 39 private import gobject.c.functions; 40 public import gobject.c.types; 41 private import linker.Loader; 42 private import std.traits; 43 44 45 /** 46 * The base object type. 47 * 48 * All the fields in the `GObject` structure are private to the implementation 49 * and should never be accessed directly. 50 * 51 * Since GLib 2.72, all #GObjects are guaranteed to be aligned to at least the 52 * alignment of the largest basic GLib type (typically this is #guint64 or 53 * #gdouble). If you need larger alignment for an element in a #GObject, you 54 * should allocate it on the heap (aligned), or arrange for your #GObject to be 55 * appropriately padded. This guarantee applies to the #GObject (or derived) 56 * struct, the #GObjectClass (or derived) struct, and any private data allocated 57 * by G_ADD_PRIVATE(). 58 */ 59 public class ObjectG 60 { 61 /** the main Gtk struct */ 62 protected GObject* gObject; 63 protected bool ownedRef; 64 65 /** Get the main Gtk struct */ 66 public GObject* getObjectGStruct(bool transferOwnership = false) 67 { 68 if (transferOwnership) 69 ownedRef = false; 70 return gObject; 71 } 72 73 /** the main Gtk struct as a void* */ 74 protected void* getStruct() 75 { 76 return cast(void*)gObject; 77 } 78 79 protected bool isGcRoot; 80 package DClosure[gulong] signals; 81 82 /** 83 * Sets our main struct and passes store it on the gobject. 84 * Add a gabage collector root to the gtk+ struct so it doesn't get collect 85 */ 86 public this (GObject* gObject, bool ownedRef = false) 87 { 88 this.gObject = gObject; 89 if ( gObject !is null ) 90 { 91 setDataFull("GObject", cast(void*)this, cast(GDestroyNotify)&destroyNotify); 92 addToggleRef(cast(GToggleNotify)&toggleNotify, cast(void*)this); 93 94 //If the refCount is larger then 1 toggleNotify isn't called 95 if (gObject.refCount > 1 && !isGcRoot) 96 { 97 GC.addRoot(cast(void*)this); 98 isGcRoot = true; 99 } 100 101 //Remove the floating reference if there is one. 102 if ( isFloating() ) 103 { 104 refSink(); 105 unref(); 106 } 107 //If we already owned this reference remove the one added by addToggleRef. 108 else if ( ownedRef ) 109 { 110 unref(); 111 } 112 } 113 } 114 115 extern(C) 116 { 117 static void destroyNotify(ObjectG obj) 118 { 119 if ( obj.isGcRoot ) 120 { 121 GC.removeRoot(cast(void*)obj); 122 obj.isGcRoot = false; 123 } 124 125 if ( obj.hasToggleRef() ) 126 obj.removeToggleRef(cast(GToggleNotify)&toggleNotify, cast(void*)obj); 127 128 obj.gObject = null; 129 } 130 131 static void toggleNotify(ObjectG obj, GObject* object, int isLastRef) 132 { 133 if ( isLastRef && obj.isGcRoot ) 134 { 135 GC.removeRoot(cast(void*)obj); 136 obj.isGcRoot = false; 137 } 138 else if ( !obj.isGcRoot ) 139 { 140 GC.addRoot(cast(void*)obj); 141 obj.isGcRoot = true; 142 } 143 } 144 } 145 146 ~this() 147 { 148 static if ( isPointer!(typeof(g_object_steal_data)) ) 149 bool libLoaded = Linker.isLoaded(LIBRARY_GOBJECT[0]); 150 else 151 enum libLoaded = true; 152 153 if ( libLoaded && gObject !is null ) 154 { 155 // Remove the GDestroyNotify callback, 156 // for when the D object is destroyed before the C one. 157 g_object_steal_data(gObject, cast(char*)"GObject"); 158 159 if ( isGcRoot ) 160 { 161 GC.removeRoot(cast(void*)this); 162 isGcRoot = false; 163 } 164 165 if ( hasToggleRef() ) 166 g_object_remove_toggle_ref(gObject, cast(GToggleNotify)&toggleNotify, cast(void*)this); 167 else 168 g_object_unref(gObject); 169 } 170 } 171 172 /** */ 173 T opCast(T)() 174 { 175 if ( !this ) 176 return null; 177 178 static if ( is(T : ObjectG) 179 && !is(T == interface) 180 && is(typeof(new T(cast(typeof(T.tupleof[0]))gObject, false))) ) 181 { 182 //If a regular cast works, return the result. 183 if ( auto r = cast(T)super ) 184 return r; 185 186 //Prints a warning if the cast is invalid. 187 //g_type_check_instance_cast(cast(GTypeInstance*)gObject, T.getType()); 188 189 //Can we cast this type to T. 190 if ( !g_type_is_a(gObject.gTypeInstance.gClass.gType, T.getType()) ) 191 return null; 192 193 //Remove the GDestroyNotify callback for the original d object. 194 g_object_steal_data(gObject, "GObject"); 195 //Remove the original object as a GC root if needed. 196 if ( isGcRoot ) 197 { 198 GC.removeRoot(cast(void*)this); 199 isGcRoot = false; 200 } 201 202 if ( hasToggleRef() ) 203 { 204 //Add a reference for the original D object before we remove the toggle reference. 205 g_object_ref(gObject); 206 g_object_remove_toggle_ref(gObject, cast(GToggleNotify)&toggleNotify, cast(void*)this); 207 } 208 209 //The new object handles the memory management. 210 return new T(cast(typeof(T.tupleof[0]))gObject, false); 211 } 212 else static if ( is(T == interface) 213 && hasStaticMember!(T, "getType") 214 && is(ReturnType!(T.getType) == GType) ) 215 { 216 //If a regular cast works, return the result. 217 if ( auto r = cast(T)super ) 218 return r; 219 220 //Do we implement interface T. 221 if ( !g_type_is_a(gObject.gTypeInstance.gClass.gType, T.getType()) ) 222 return null; 223 224 return getInterfaceInstance!T(gObject); 225 } 226 else 227 return cast(T)super; 228 } 229 230 unittest 231 { 232 ObjectG obj = null; 233 234 assert( (cast(Binding)obj) is null ); 235 } 236 237 /** 238 * Gets a D Object from the objects table of associations. 239 * Params: 240 * obj = GObject containing the associations. 241 * Returns: the D Object if found, or a newly constructed object if no such Object exists. 242 */ 243 public static RT getDObject(T, RT=T, U)(U obj, bool ownedRef = false) 244 { 245 if ( obj is null ) 246 { 247 return null; 248 } 249 250 static if ( is(T : ObjectG) && !is(RT == interface) ) 251 { 252 auto p = g_object_get_data(cast(GObject*)obj, Str.toStringz("GObject")); 253 254 if ( p !is null ) 255 return cast(RT)cast(ObjectG)p; 256 else 257 return new T(obj, ownedRef); 258 } 259 else static if ( is(RT == interface) && hasMember!(RT, "getType") && is(ReturnType!(RT.getType) == GType) ) 260 { 261 auto p = g_object_get_data(cast(GObject*)obj, Str.toStringz("GObject")); 262 263 if ( p !is null ) 264 return cast(RT)cast(ObjectG)p; 265 else 266 return getInterfaceInstance!RT(cast(GObject*)obj); 267 } 268 else static if ( is(typeof(new T(obj, ownedRef))) ) 269 { 270 return new T(obj, ownedRef); 271 } 272 else 273 { 274 return new T(obj); 275 } 276 } 277 278 private static I getInterfaceInstance(I)(GObject* instance) 279 { 280 static class Impl: ObjectG, I 281 { 282 public this (GObject* gObject, bool ownedRef = false) 283 { 284 super(gObject, ownedRef); 285 } 286 287 /** the main Gtk struct as a void* */ 288 protected override void* getStruct() 289 { 290 return cast(void*)gObject; 291 } 292 293 // add the interface capabilities 294 mixin("import "~ moduleName!I[0..$-2] ~"T;import "~ moduleName!I ~"; mixin "~ __traits(identifier, I)[0..$-2] ~"T!("~__traits(identifier, Impl)~");"); 295 } 296 297 ClassInfo ci = Impl.classinfo; 298 Impl iface; 299 void* p; 300 301 //Skip all the setup for the memory management, 302 //and only add an extra reference for the instance returned. 303 p = GC.malloc(ci.initializer.length, GC.BlkAttr.FINALIZE, ci); 304 p[0..ci.initializer.length] = ci.initializer; 305 iface = cast(Impl)p; 306 iface.gObject = instance; 307 iface.doref(); 308 309 return iface; 310 } 311 312 /** 313 * Is there a toggle ref connected to this object. 314 */ 315 private bool hasToggleRef() 316 { 317 enum TOGGLE_REF_FLAG = 0x1; 318 319 if ( (g_datalist_get_flags(&gObject.qdata) & TOGGLE_REF_FLAG) != 0 ) 320 //TODO: Assumes we always have the gObject data set if the toggleRef is connected to this instance. 321 return (g_object_get_data(gObject, cast(char*)"GObject") is cast(void*)this); 322 else 323 return false; 324 } 325 326 public void removeGcRoot() 327 { 328 if ( hasToggleRef() ) 329 { 330 g_object_ref(gObject); 331 g_object_remove_toggle_ref(gObject, cast(GToggleNotify)&toggleNotify, cast(void*)this); 332 } 333 334 if ( isGcRoot ) 335 { 336 GC.removeRoot(cast(void*)this); 337 isGcRoot = false; 338 } 339 } 340 341 /** */ 342 public void setProperty(T)(string propertyName, T value) 343 { 344 setProperty(propertyName, new Value(value)); 345 } 346 347 deprecated("Use the member function") 348 public static void unref(ObjectG obj) 349 { 350 obj.unref(); 351 } 352 353 deprecated("Use the member function") 354 public static ObjectG doref(ObjectG obj) 355 { 356 return obj.doref(); 357 } 358 359 /** 360 * The notify signal is emitted on an object when one of its 361 * properties has been changed. Note that getting this signal 362 * doesn't guarantee that the value of the property has actually 363 * changed, it may also be emitted when the setter for the property 364 * is called to reinstate the previous value. 365 * 366 * This signal is typically used to obtain change notification for a 367 * single property. 368 * 369 * It is important to note that you must use 370 * canonical parameter names for the property. 371 * 372 * Params: 373 * dlg = The callback. 374 * property = Set this if you only want to receive the signal for a specific property. 375 * connectFlags = The behavior of the signal's connection. 376 */ 377 gulong addOnNotify(void delegate(ParamSpec, ObjectG) dlg, string property = "", ConnectFlags connectFlags=cast(ConnectFlags)0) 378 { 379 string signalName; 380 381 if ( property == "" ) 382 signalName = "notify"; 383 else 384 signalName = "notify::"~ property; 385 386 return Signals.connect(this, signalName, dlg, connectFlags ^ ConnectFlags.SWAPPED); 387 } 388 389 /** 390 */ 391 392 /** */ 393 public static GType getType() 394 { 395 return g_initially_unowned_get_type(); 396 } 397 398 /** 399 * Creates a new instance of a #GObject subtype and sets its properties. 400 * 401 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY) 402 * which are not explicitly specified are set to their default values. 403 * 404 * Params: 405 * objectType = the type id of the #GObject subtype to instantiate 406 * firstPropertyName = the name of the first property 407 * varArgs = the value of the first property, followed optionally by more 408 * name/value pairs, followed by %NULL 409 * 410 * Returns: a new instance of @object_type 411 * 412 * Throws: ConstructionException GTK+ fails to create the object. 413 */ 414 public this(GType objectType, string firstPropertyName, void* varArgs) 415 { 416 auto __p = g_object_new_valist(objectType, Str.toStringz(firstPropertyName), varArgs); 417 418 if(__p is null) 419 { 420 throw new ConstructionException("null returned by new_valist"); 421 } 422 423 this(cast(GObject*) __p, true); 424 } 425 426 /** 427 * Creates a new instance of a #GObject subtype and sets its properties using 428 * the provided arrays. Both arrays must have exactly @n_properties elements, 429 * and the names and values correspond by index. 430 * 431 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY) 432 * which are not explicitly specified are set to their default values. 433 * 434 * Params: 435 * objectType = the object type to instantiate 436 * names = the names of each property to be set 437 * values = the values of each property to be set 438 * 439 * Returns: a new instance of 440 * @object_type 441 * 442 * Since: 2.54 443 * 444 * Throws: ConstructionException GTK+ fails to create the object. 445 */ 446 public this(GType objectType, string[] names, Value[] values) 447 { 448 GValue[] valuesArray = new GValue[values.length]; 449 for ( int i = 0; i < values.length; i++ ) 450 { 451 valuesArray[i] = *(values[i].getValueStruct()); 452 } 453 454 auto __p = g_object_new_with_properties(objectType, cast(uint)values.length, Str.toStringzArray(names), valuesArray.ptr); 455 456 if(__p is null) 457 { 458 throw new ConstructionException("null returned by new_with_properties"); 459 } 460 461 this(cast(GObject*) __p, true); 462 } 463 464 /** 465 * Creates a new instance of a #GObject subtype and sets its properties. 466 * 467 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY) 468 * which are not explicitly specified are set to their default values. 469 * 470 * Deprecated: Use g_object_new_with_properties() instead. 471 * deprecated. See #GParameter for more information. 472 * 473 * Params: 474 * objectType = the type id of the #GObject subtype to instantiate 475 * parameters = an array of #GParameter 476 * 477 * Returns: a new instance of 478 * @object_type 479 * 480 * Throws: ConstructionException GTK+ fails to create the object. 481 */ 482 public this(GType objectType, GParameter[] parameters) 483 { 484 auto __p = g_object_newv(objectType, cast(uint)parameters.length, parameters.ptr); 485 486 if(__p is null) 487 { 488 throw new ConstructionException("null returned by newv"); 489 } 490 491 this(cast(GObject*) __p, true); 492 } 493 494 /** */ 495 public static size_t compatControl(size_t what, void* data) 496 { 497 return g_object_compat_control(what, data); 498 } 499 500 /** 501 * Find the #GParamSpec with the given name for an 502 * interface. Generally, the interface vtable passed in as @g_iface 503 * will be the default vtable from g_type_default_interface_ref(), or, 504 * if you know the interface has already been loaded, 505 * g_type_default_interface_peek(). 506 * 507 * Params: 508 * gIface = any interface vtable for the 509 * interface, or the default vtable for the interface 510 * propertyName = name of a property to look up. 511 * 512 * Returns: the #GParamSpec for the property of the 513 * interface with the name @property_name, or %NULL if no 514 * such property exists. 515 * 516 * Since: 2.4 517 */ 518 public static ParamSpec interfaceFindProperty(TypeInterface gIface, string propertyName) 519 { 520 auto __p = g_object_interface_find_property((gIface is null) ? null : gIface.getTypeInterfaceStruct(), Str.toStringz(propertyName)); 521 522 if(__p is null) 523 { 524 return null; 525 } 526 527 return ObjectG.getDObject!(ParamSpec)(cast(GParamSpec*) __p); 528 } 529 530 /** 531 * Add a property to an interface; this is only useful for interfaces 532 * that are added to GObject-derived types. Adding a property to an 533 * interface forces all objects classes with that interface to have a 534 * compatible property. The compatible property could be a newly 535 * created #GParamSpec, but normally 536 * g_object_class_override_property() will be used so that the object 537 * class only needs to provide an implementation and inherits the 538 * property description, default value, bounds, and so forth from the 539 * interface property. 540 * 541 * This function is meant to be called from the interface's default 542 * vtable initialization function (the @class_init member of 543 * #GTypeInfo.) It must not be called after after @class_init has 544 * been called for any object types implementing this interface. 545 * 546 * If @pspec is a floating reference, it will be consumed. 547 * 548 * Params: 549 * gIface = any interface vtable for the 550 * interface, or the default 551 * vtable for the interface. 552 * pspec = the #GParamSpec for the new property 553 * 554 * Since: 2.4 555 */ 556 public static void interfaceInstallProperty(TypeInterface gIface, ParamSpec pspec) 557 { 558 g_object_interface_install_property((gIface is null) ? null : gIface.getTypeInterfaceStruct(), (pspec is null) ? null : pspec.getParamSpecStruct()); 559 } 560 561 /** 562 * Lists the properties of an interface.Generally, the interface 563 * vtable passed in as @g_iface will be the default vtable from 564 * g_type_default_interface_ref(), or, if you know the interface has 565 * already been loaded, g_type_default_interface_peek(). 566 * 567 * Params: 568 * gIface = any interface vtable for the 569 * interface, or the default vtable for the interface 570 * 571 * Returns: a 572 * pointer to an array of pointers to #GParamSpec 573 * structures. The paramspecs are owned by GLib, but the 574 * array should be freed with g_free() when you are done with 575 * it. 576 * 577 * Since: 2.4 578 */ 579 public static ParamSpec[] interfaceListProperties(TypeInterface gIface) 580 { 581 uint nPropertiesP; 582 583 auto __p = g_object_interface_list_properties((gIface is null) ? null : gIface.getTypeInterfaceStruct(), &nPropertiesP); 584 585 if(__p is null) 586 { 587 return null; 588 } 589 590 ParamSpec[] arr = new ParamSpec[nPropertiesP]; 591 for(int i = 0; i < nPropertiesP; i++) 592 { 593 arr[i] = ObjectG.getDObject!(ParamSpec)(cast(GParamSpec*) __p[i]); 594 } 595 596 return arr; 597 } 598 599 /** 600 * Increases the reference count of the object by one and sets a 601 * callback to be called when all other references to the object are 602 * dropped, or when this is already the last reference to the object 603 * and another reference is established. 604 * 605 * This functionality is intended for binding @object to a proxy 606 * object managed by another memory manager. This is done with two 607 * paired references: the strong reference added by 608 * g_object_add_toggle_ref() and a reverse reference to the proxy 609 * object which is either a strong reference or weak reference. 610 * 611 * The setup is that when there are no other references to @object, 612 * only a weak reference is held in the reverse direction from @object 613 * to the proxy object, but when there are other references held to 614 * @object, a strong reference is held. The @notify callback is called 615 * when the reference from @object to the proxy object should be 616 * "toggled" from strong to weak (@is_last_ref true) or weak to strong 617 * (@is_last_ref false). 618 * 619 * Since a (normal) reference must be held to the object before 620 * calling g_object_add_toggle_ref(), the initial state of the reverse 621 * link is always strong. 622 * 623 * Multiple toggle references may be added to the same gobject, 624 * however if there are multiple toggle references to an object, none 625 * of them will ever be notified until all but one are removed. For 626 * this reason, you should only ever use a toggle reference if there 627 * is important state in the proxy object. 628 * 629 * Params: 630 * notify = a function to call when this reference is the 631 * last reference to the object, or is no longer 632 * the last reference. 633 * data = data to pass to @notify 634 * 635 * Since: 2.8 636 */ 637 public void addToggleRef(GToggleNotify notify, void* data) 638 { 639 g_object_add_toggle_ref(gObject, notify, data); 640 } 641 642 /** 643 * Adds a weak reference from weak_pointer to @object to indicate that 644 * the pointer located at @weak_pointer_location is only valid during 645 * the lifetime of @object. When the @object is finalized, 646 * @weak_pointer will be set to %NULL. 647 * 648 * Note that as with g_object_weak_ref(), the weak references created by 649 * this method are not thread-safe: they cannot safely be used in one 650 * thread if the object's last g_object_unref() might happen in another 651 * thread. Use #GWeakRef if thread-safety is required. 652 * 653 * Params: 654 * weakPointerLocation = The memory address 655 * of a pointer. 656 */ 657 public void addWeakPointer(ref void* weakPointerLocation) 658 { 659 g_object_add_weak_pointer(gObject, &weakPointerLocation); 660 } 661 662 /** 663 * Creates a binding between @source_property on @source and @target_property 664 * on @target. 665 * 666 * Whenever the @source_property is changed the @target_property is 667 * updated using the same value. For instance: 668 * 669 * |[<!-- language="C" --> 670 * g_object_bind_property (action, "active", widget, "sensitive", 0); 671 * ]| 672 * 673 * Will result in the "sensitive" property of the widget #GObject instance to be 674 * updated with the same value of the "active" property of the action #GObject 675 * instance. 676 * 677 * If @flags contains %G_BINDING_BIDIRECTIONAL then the binding will be mutual: 678 * if @target_property on @target changes then the @source_property on @source 679 * will be updated as well. 680 * 681 * The binding will automatically be removed when either the @source or the 682 * @target instances are finalized. To remove the binding without affecting the 683 * @source and the @target you can just call g_object_unref() on the returned 684 * #GBinding instance. 685 * 686 * Removing the binding by calling g_object_unref() on it must only be done if 687 * the binding, @source and @target are only used from a single thread and it 688 * is clear that both @source and @target outlive the binding. Especially it 689 * is not safe to rely on this if the binding, @source or @target can be 690 * finalized from different threads. Keep another reference to the binding and 691 * use g_binding_unbind() instead to be on the safe side. 692 * 693 * A #GObject can have multiple bindings. 694 * 695 * Params: 696 * sourceProperty = the property on @source to bind 697 * target = the target #GObject 698 * targetProperty = the property on @target to bind 699 * flags = flags to pass to #GBinding 700 * 701 * Returns: the #GBinding instance representing the 702 * binding between the two #GObject instances. The binding is released 703 * whenever the #GBinding reference count reaches zero. 704 * 705 * Since: 2.26 706 */ 707 public Binding bindProperty(string sourceProperty, ObjectG target, string targetProperty, GBindingFlags flags) 708 { 709 auto __p = g_object_bind_property(gObject, Str.toStringz(sourceProperty), (target is null) ? null : target.getObjectGStruct(), Str.toStringz(targetProperty), flags); 710 711 if(__p is null) 712 { 713 return null; 714 } 715 716 return ObjectG.getDObject!(Binding)(cast(GBinding*) __p); 717 } 718 719 /** 720 * Complete version of g_object_bind_property(). 721 * 722 * Creates a binding between @source_property on @source and @target_property 723 * on @target, allowing you to set the transformation functions to be used by 724 * the binding. 725 * 726 * If @flags contains %G_BINDING_BIDIRECTIONAL then the binding will be mutual: 727 * if @target_property on @target changes then the @source_property on @source 728 * will be updated as well. The @transform_from function is only used in case 729 * of bidirectional bindings, otherwise it will be ignored 730 * 731 * The binding will automatically be removed when either the @source or the 732 * @target instances are finalized. This will release the reference that is 733 * being held on the #GBinding instance; if you want to hold on to the 734 * #GBinding instance, you will need to hold a reference to it. 735 * 736 * To remove the binding, call g_binding_unbind(). 737 * 738 * A #GObject can have multiple bindings. 739 * 740 * The same @user_data parameter will be used for both @transform_to 741 * and @transform_from transformation functions; the @notify function will 742 * be called once, when the binding is removed. If you need different data 743 * for each transformation function, please use 744 * g_object_bind_property_with_closures() instead. 745 * 746 * Params: 747 * sourceProperty = the property on @source to bind 748 * target = the target #GObject 749 * targetProperty = the property on @target to bind 750 * flags = flags to pass to #GBinding 751 * transformTo = the transformation function 752 * from the @source to the @target, or %NULL to use the default 753 * transformFrom = the transformation function 754 * from the @target to the @source, or %NULL to use the default 755 * userData = custom data to be passed to the transformation functions, 756 * or %NULL 757 * notify = a function to call when disposing the binding, to free 758 * resources used by the transformation functions, or %NULL if not required 759 * 760 * Returns: the #GBinding instance representing the 761 * binding between the two #GObject instances. The binding is released 762 * whenever the #GBinding reference count reaches zero. 763 * 764 * Since: 2.26 765 */ 766 public Binding bindPropertyFull(string sourceProperty, ObjectG target, string targetProperty, GBindingFlags flags, GBindingTransformFunc transformTo, GBindingTransformFunc transformFrom, void* userData, GDestroyNotify notify) 767 { 768 auto __p = g_object_bind_property_full(gObject, Str.toStringz(sourceProperty), (target is null) ? null : target.getObjectGStruct(), Str.toStringz(targetProperty), flags, transformTo, transformFrom, userData, notify); 769 770 if(__p is null) 771 { 772 return null; 773 } 774 775 return ObjectG.getDObject!(Binding)(cast(GBinding*) __p); 776 } 777 778 /** 779 * Creates a binding between @source_property on @source and @target_property 780 * on @target, allowing you to set the transformation functions to be used by 781 * the binding. 782 * 783 * This function is the language bindings friendly version of 784 * g_object_bind_property_full(), using #GClosures instead of 785 * function pointers. 786 * 787 * Params: 788 * sourceProperty = the property on @source to bind 789 * target = the target #GObject 790 * targetProperty = the property on @target to bind 791 * flags = flags to pass to #GBinding 792 * transformTo = a #GClosure wrapping the transformation function 793 * from the @source to the @target, or %NULL to use the default 794 * transformFrom = a #GClosure wrapping the transformation function 795 * from the @target to the @source, or %NULL to use the default 796 * 797 * Returns: the #GBinding instance representing the 798 * binding between the two #GObject instances. The binding is released 799 * whenever the #GBinding reference count reaches zero. 800 * 801 * Since: 2.26 802 */ 803 public Binding bindPropertyWithClosures(string sourceProperty, ObjectG target, string targetProperty, GBindingFlags flags, Closure transformTo, Closure transformFrom) 804 { 805 auto __p = g_object_bind_property_with_closures(gObject, Str.toStringz(sourceProperty), (target is null) ? null : target.getObjectGStruct(), Str.toStringz(targetProperty), flags, (transformTo is null) ? null : transformTo.getClosureStruct(), (transformFrom is null) ? null : transformFrom.getClosureStruct()); 806 807 if(__p is null) 808 { 809 return null; 810 } 811 812 return ObjectG.getDObject!(Binding)(cast(GBinding*) __p); 813 } 814 815 /** 816 * This is a variant of g_object_get_data() which returns 817 * a 'duplicate' of the value. @dup_func defines the 818 * meaning of 'duplicate' in this context, it could e.g. 819 * take a reference on a ref-counted object. 820 * 821 * If the @key is not set on the object then @dup_func 822 * will be called with a %NULL argument. 823 * 824 * Note that @dup_func is called while user data of @object 825 * is locked. 826 * 827 * This function can be useful to avoid races when multiple 828 * threads are using object data on the same key on the same 829 * object. 830 * 831 * Params: 832 * key = a string, naming the user data pointer 833 * dupFunc = function to dup the value 834 * userData = passed as user_data to @dup_func 835 * 836 * Returns: the result of calling @dup_func on the value 837 * associated with @key on @object, or %NULL if not set. 838 * If @dup_func is %NULL, the value is returned 839 * unmodified. 840 * 841 * Since: 2.34 842 */ 843 public void* dupData(string key, GDuplicateFunc dupFunc, void* userData) 844 { 845 return g_object_dup_data(gObject, Str.toStringz(key), dupFunc, userData); 846 } 847 848 /** 849 * This is a variant of g_object_get_qdata() which returns 850 * a 'duplicate' of the value. @dup_func defines the 851 * meaning of 'duplicate' in this context, it could e.g. 852 * take a reference on a ref-counted object. 853 * 854 * If the @quark is not set on the object then @dup_func 855 * will be called with a %NULL argument. 856 * 857 * Note that @dup_func is called while user data of @object 858 * is locked. 859 * 860 * This function can be useful to avoid races when multiple 861 * threads are using object data on the same key on the same 862 * object. 863 * 864 * Params: 865 * quark = a #GQuark, naming the user data pointer 866 * dupFunc = function to dup the value 867 * userData = passed as user_data to @dup_func 868 * 869 * Returns: the result of calling @dup_func on the value 870 * associated with @quark on @object, or %NULL if not set. 871 * If @dup_func is %NULL, the value is returned 872 * unmodified. 873 * 874 * Since: 2.34 875 */ 876 public void* dupQdata(GQuark quark, GDuplicateFunc dupFunc, void* userData) 877 { 878 return g_object_dup_qdata(gObject, quark, dupFunc, userData); 879 } 880 881 /** 882 * This function is intended for #GObject implementations to re-enforce 883 * a [floating][floating-ref] object reference. Doing this is seldom 884 * required: all #GInitiallyUnowneds are created with a floating reference 885 * which usually just needs to be sunken by calling g_object_ref_sink(). 886 * 887 * Since: 2.10 888 */ 889 public void forceFloating() 890 { 891 g_object_force_floating(gObject); 892 } 893 894 /** 895 * Increases the freeze count on @object. If the freeze count is 896 * non-zero, the emission of "notify" signals on @object is 897 * stopped. The signals are queued until the freeze count is decreased 898 * to zero. Duplicate notifications are squashed so that at most one 899 * #GObject::notify signal is emitted for each property modified while the 900 * object is frozen. 901 * 902 * This is necessary for accessors that modify multiple properties to prevent 903 * premature notification while the object is still being modified. 904 */ 905 public void freezeNotify() 906 { 907 g_object_freeze_notify(gObject); 908 } 909 910 /** 911 * Gets a named field from the objects table of associations (see g_object_set_data()). 912 * 913 * Params: 914 * key = name of the key for that association 915 * 916 * Returns: the data if found, 917 * or %NULL if no such data exists. 918 */ 919 public void* getData(string key) 920 { 921 return g_object_get_data(gObject, Str.toStringz(key)); 922 } 923 924 /** 925 * Gets a property of an object. 926 * 927 * The @value can be: 928 * 929 * - an empty #GValue initialized by %G_VALUE_INIT, which will be 930 * automatically initialized with the expected type of the property 931 * (since GLib 2.60) 932 * - a #GValue initialized with the expected type of the property 933 * - a #GValue initialized with a type to which the expected type 934 * of the property can be transformed 935 * 936 * In general, a copy is made of the property contents and the caller is 937 * responsible for freeing the memory by calling g_value_unset(). 938 * 939 * Note that g_object_get_property() is really intended for language 940 * bindings, g_object_get() is much more convenient for C programming. 941 * 942 * Params: 943 * propertyName = the name of the property to get 944 * value = return location for the property value 945 */ 946 public void getProperty(string propertyName, Value value) 947 { 948 g_object_get_property(gObject, Str.toStringz(propertyName), (value is null) ? null : value.getValueStruct()); 949 } 950 951 /** 952 * This function gets back user data pointers stored via 953 * g_object_set_qdata(). 954 * 955 * Params: 956 * quark = A #GQuark, naming the user data pointer 957 * 958 * Returns: The user data pointer set, or %NULL 959 */ 960 public void* getQdata(GQuark quark) 961 { 962 return g_object_get_qdata(gObject, quark); 963 } 964 965 /** 966 * Gets properties of an object. 967 * 968 * In general, a copy is made of the property contents and the caller 969 * is responsible for freeing the memory in the appropriate manner for 970 * the type, for instance by calling g_free() or g_object_unref(). 971 * 972 * See g_object_get(). 973 * 974 * Params: 975 * firstPropertyName = name of the first property to get 976 * varArgs = return location for the first property, followed optionally by more 977 * name/return location pairs, followed by %NULL 978 */ 979 public void getValist(string firstPropertyName, void* varArgs) 980 { 981 g_object_get_valist(gObject, Str.toStringz(firstPropertyName), varArgs); 982 } 983 984 /** 985 * Gets @n_properties properties for an @object. 986 * Obtained properties will be set to @values. All properties must be valid. 987 * Warnings will be emitted and undefined behaviour may result if invalid 988 * properties are passed in. 989 * 990 * Params: 991 * names = the names of each property to get 992 * values = the values of each property to get 993 * 994 * Since: 2.54 995 */ 996 public void getv(string[] names, Value[] values) 997 { 998 GValue[] valuesArray = new GValue[values.length]; 999 for ( int i = 0; i < values.length; i++ ) 1000 { 1001 valuesArray[i] = *(values[i].getValueStruct()); 1002 } 1003 1004 g_object_getv(gObject, cast(uint)values.length, Str.toStringzArray(names), valuesArray.ptr); 1005 } 1006 1007 /** 1008 * Checks whether @object has a [floating][floating-ref] reference. 1009 * 1010 * Returns: %TRUE if @object has a floating reference 1011 * 1012 * Since: 2.10 1013 */ 1014 public bool isFloating() 1015 { 1016 return g_object_is_floating(gObject) != 0; 1017 } 1018 1019 /** 1020 * Emits a "notify" signal for the property @property_name on @object. 1021 * 1022 * When possible, eg. when signaling a property change from within the class 1023 * that registered the property, you should use g_object_notify_by_pspec() 1024 * instead. 1025 * 1026 * Note that emission of the notify signal may be blocked with 1027 * g_object_freeze_notify(). In this case, the signal emissions are queued 1028 * and will be emitted (in reverse order) when g_object_thaw_notify() is 1029 * called. 1030 * 1031 * Params: 1032 * propertyName = the name of a property installed on the class of @object. 1033 */ 1034 public void notify(string propertyName) 1035 { 1036 g_object_notify(gObject, Str.toStringz(propertyName)); 1037 } 1038 1039 /** 1040 * Emits a "notify" signal for the property specified by @pspec on @object. 1041 * 1042 * This function omits the property name lookup, hence it is faster than 1043 * g_object_notify(). 1044 * 1045 * One way to avoid using g_object_notify() from within the 1046 * class that registered the properties, and using g_object_notify_by_pspec() 1047 * instead, is to store the GParamSpec used with 1048 * g_object_class_install_property() inside a static array, e.g.: 1049 * 1050 * |[<!-- language="C" --> 1051 * enum 1052 * { 1053 * PROP_0, 1054 * PROP_FOO, 1055 * PROP_LAST 1056 * }; 1057 * 1058 * static GParamSpec *properties[PROP_LAST]; 1059 * 1060 * static void 1061 * my_object_class_init (MyObjectClass *klass) 1062 * { 1063 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo", 1064 * 0, 100, 1065 * 50, 1066 * G_PARAM_READWRITE); 1067 * g_object_class_install_property (gobject_class, 1068 * PROP_FOO, 1069 * properties[PROP_FOO]); 1070 * } 1071 * ]| 1072 * 1073 * and then notify a change on the "foo" property with: 1074 * 1075 * |[<!-- language="C" --> 1076 * g_object_notify_by_pspec (self, properties[PROP_FOO]); 1077 * ]| 1078 * 1079 * Params: 1080 * pspec = the #GParamSpec of a property installed on the class of @object. 1081 * 1082 * Since: 2.26 1083 */ 1084 public void notifyByPspec(ParamSpec pspec) 1085 { 1086 g_object_notify_by_pspec(gObject, (pspec is null) ? null : pspec.getParamSpecStruct()); 1087 } 1088 1089 alias doref = ref_; 1090 /** 1091 * Increases the reference count of @object. 1092 * 1093 * Since GLib 2.56, if `GLIB_VERSION_MAX_ALLOWED` is 2.56 or greater, the type 1094 * of @object will be propagated to the return type (using the GCC typeof() 1095 * extension), so any casting the caller needs to do on the return type must be 1096 * explicit. 1097 * 1098 * Returns: the same @object 1099 */ 1100 public ObjectG ref_() 1101 { 1102 auto __p = g_object_ref(gObject); 1103 1104 if(__p is null) 1105 { 1106 return null; 1107 } 1108 1109 return ObjectG.getDObject!(ObjectG)(cast(GObject*) __p); 1110 } 1111 1112 /** 1113 * Increase the reference count of @object, and possibly remove the 1114 * [floating][floating-ref] reference, if @object has a floating reference. 1115 * 1116 * In other words, if the object is floating, then this call "assumes 1117 * ownership" of the floating reference, converting it to a normal 1118 * reference by clearing the floating flag while leaving the reference 1119 * count unchanged. If the object is not floating, then this call 1120 * adds a new normal reference increasing the reference count by one. 1121 * 1122 * Since GLib 2.56, the type of @object will be propagated to the return type 1123 * under the same conditions as for g_object_ref(). 1124 * 1125 * Returns: @object 1126 * 1127 * Since: 2.10 1128 */ 1129 public ObjectG refSink() 1130 { 1131 auto __p = g_object_ref_sink(gObject); 1132 1133 if(__p is null) 1134 { 1135 return null; 1136 } 1137 1138 return ObjectG.getDObject!(ObjectG)(cast(GObject*) __p); 1139 } 1140 1141 /** 1142 * Removes a reference added with g_object_add_toggle_ref(). The 1143 * reference count of the object is decreased by one. 1144 * 1145 * Params: 1146 * notify = a function to call when this reference is the 1147 * last reference to the object, or is no longer 1148 * the last reference. 1149 * data = data to pass to @notify, or %NULL to 1150 * match any toggle refs with the @notify argument. 1151 * 1152 * Since: 2.8 1153 */ 1154 public void removeToggleRef(GToggleNotify notify, void* data) 1155 { 1156 g_object_remove_toggle_ref(gObject, notify, data); 1157 } 1158 1159 /** 1160 * Removes a weak reference from @object that was previously added 1161 * using g_object_add_weak_pointer(). The @weak_pointer_location has 1162 * to match the one used with g_object_add_weak_pointer(). 1163 * 1164 * Params: 1165 * weakPointerLocation = The memory address 1166 * of a pointer. 1167 */ 1168 public void removeWeakPointer(ref void* weakPointerLocation) 1169 { 1170 g_object_remove_weak_pointer(gObject, &weakPointerLocation); 1171 } 1172 1173 /** 1174 * Compares the user data for the key @key on @object with 1175 * @oldval, and if they are the same, replaces @oldval with 1176 * @newval. 1177 * 1178 * This is like a typical atomic compare-and-exchange 1179 * operation, for user data on an object. 1180 * 1181 * If the previous value was replaced then ownership of the 1182 * old value (@oldval) is passed to the caller, including 1183 * the registered destroy notify for it (passed out in @old_destroy). 1184 * It’s up to the caller to free this as needed, which may 1185 * or may not include using @old_destroy as sometimes replacement 1186 * should not destroy the object in the normal way. 1187 * 1188 * See g_object_set_data() for guidance on using a small, bounded set of values 1189 * for @key. 1190 * 1191 * Params: 1192 * key = a string, naming the user data pointer 1193 * oldval = the old value to compare against 1194 * newval = the new value 1195 * destroy = a destroy notify for the new value 1196 * oldDestroy = destroy notify for the existing value 1197 * 1198 * Returns: %TRUE if the existing value for @key was replaced 1199 * by @newval, %FALSE otherwise. 1200 * 1201 * Since: 2.34 1202 */ 1203 public bool replaceData(string key, void* oldval, void* newval, GDestroyNotify destroy, out GDestroyNotify oldDestroy) 1204 { 1205 return g_object_replace_data(gObject, Str.toStringz(key), oldval, newval, destroy, &oldDestroy) != 0; 1206 } 1207 1208 /** 1209 * Compares the user data for the key @quark on @object with 1210 * @oldval, and if they are the same, replaces @oldval with 1211 * @newval. 1212 * 1213 * This is like a typical atomic compare-and-exchange 1214 * operation, for user data on an object. 1215 * 1216 * If the previous value was replaced then ownership of the 1217 * old value (@oldval) is passed to the caller, including 1218 * the registered destroy notify for it (passed out in @old_destroy). 1219 * It’s up to the caller to free this as needed, which may 1220 * or may not include using @old_destroy as sometimes replacement 1221 * should not destroy the object in the normal way. 1222 * 1223 * Params: 1224 * quark = a #GQuark, naming the user data pointer 1225 * oldval = the old value to compare against 1226 * newval = the new value 1227 * destroy = a destroy notify for the new value 1228 * oldDestroy = destroy notify for the existing value 1229 * 1230 * Returns: %TRUE if the existing value for @quark was replaced 1231 * by @newval, %FALSE otherwise. 1232 * 1233 * Since: 2.34 1234 */ 1235 public bool replaceQdata(GQuark quark, void* oldval, void* newval, GDestroyNotify destroy, out GDestroyNotify oldDestroy) 1236 { 1237 return g_object_replace_qdata(gObject, quark, oldval, newval, destroy, &oldDestroy) != 0; 1238 } 1239 1240 /** 1241 * Releases all references to other objects. This can be used to break 1242 * reference cycles. 1243 * 1244 * This function should only be called from object system implementations. 1245 */ 1246 public void runDispose() 1247 { 1248 g_object_run_dispose(gObject); 1249 } 1250 1251 /** 1252 * Each object carries around a table of associations from 1253 * strings to pointers. This function lets you set an association. 1254 * 1255 * If the object already had an association with that name, 1256 * the old association will be destroyed. 1257 * 1258 * Internally, the @key is converted to a #GQuark using g_quark_from_string(). 1259 * This means a copy of @key is kept permanently (even after @object has been 1260 * finalized) — so it is recommended to only use a small, bounded set of values 1261 * for @key in your program, to avoid the #GQuark storage growing unbounded. 1262 * 1263 * Params: 1264 * key = name of the key 1265 * data = data to associate with that key 1266 */ 1267 public void setData(string key, void* data) 1268 { 1269 g_object_set_data(gObject, Str.toStringz(key), data); 1270 } 1271 1272 /** 1273 * Like g_object_set_data() except it adds notification 1274 * for when the association is destroyed, either by setting it 1275 * to a different value or when the object is destroyed. 1276 * 1277 * Note that the @destroy callback is not called if @data is %NULL. 1278 * 1279 * Params: 1280 * key = name of the key 1281 * data = data to associate with that key 1282 * destroy = function to call when the association is destroyed 1283 */ 1284 public void setDataFull(string key, void* data, GDestroyNotify destroy) 1285 { 1286 g_object_set_data_full(gObject, Str.toStringz(key), data, destroy); 1287 } 1288 1289 /** 1290 * Sets a property on an object. 1291 * 1292 * Params: 1293 * propertyName = the name of the property to set 1294 * value = the value 1295 */ 1296 public void setProperty(string propertyName, Value value) 1297 { 1298 g_object_set_property(gObject, Str.toStringz(propertyName), (value is null) ? null : value.getValueStruct()); 1299 } 1300 1301 /** 1302 * This sets an opaque, named pointer on an object. 1303 * The name is specified through a #GQuark (retrieved e.g. via 1304 * g_quark_from_static_string()), and the pointer 1305 * can be gotten back from the @object with g_object_get_qdata() 1306 * until the @object is finalized. 1307 * Setting a previously set user data pointer, overrides (frees) 1308 * the old pointer set, using #NULL as pointer essentially 1309 * removes the data stored. 1310 * 1311 * Params: 1312 * quark = A #GQuark, naming the user data pointer 1313 * data = An opaque user data pointer 1314 */ 1315 public void setQdata(GQuark quark, void* data) 1316 { 1317 g_object_set_qdata(gObject, quark, data); 1318 } 1319 1320 /** 1321 * This function works like g_object_set_qdata(), but in addition, 1322 * a void (*destroy) (gpointer) function may be specified which is 1323 * called with @data as argument when the @object is finalized, or 1324 * the data is being overwritten by a call to g_object_set_qdata() 1325 * with the same @quark. 1326 * 1327 * Params: 1328 * quark = A #GQuark, naming the user data pointer 1329 * data = An opaque user data pointer 1330 * destroy = Function to invoke with @data as argument, when @data 1331 * needs to be freed 1332 */ 1333 public void setQdataFull(GQuark quark, void* data, GDestroyNotify destroy) 1334 { 1335 g_object_set_qdata_full(gObject, quark, data, destroy); 1336 } 1337 1338 /** 1339 * Sets properties on an object. 1340 * 1341 * Params: 1342 * firstPropertyName = name of the first property to set 1343 * varArgs = value for the first property, followed optionally by more 1344 * name/value pairs, followed by %NULL 1345 */ 1346 public void setValist(string firstPropertyName, void* varArgs) 1347 { 1348 g_object_set_valist(gObject, Str.toStringz(firstPropertyName), varArgs); 1349 } 1350 1351 /** 1352 * Sets @n_properties properties for an @object. 1353 * Properties to be set will be taken from @values. All properties must be 1354 * valid. Warnings will be emitted and undefined behaviour may result if invalid 1355 * properties are passed in. 1356 * 1357 * Params: 1358 * names = the names of each property to be set 1359 * values = the values of each property to be set 1360 * 1361 * Since: 2.54 1362 */ 1363 public void setv(string[] names, Value[] values) 1364 { 1365 GValue[] valuesArray = new GValue[values.length]; 1366 for ( int i = 0; i < values.length; i++ ) 1367 { 1368 valuesArray[i] = *(values[i].getValueStruct()); 1369 } 1370 1371 g_object_setv(gObject, cast(uint)values.length, Str.toStringzArray(names), valuesArray.ptr); 1372 } 1373 1374 /** 1375 * Remove a specified datum from the object's data associations, 1376 * without invoking the association's destroy handler. 1377 * 1378 * Params: 1379 * key = name of the key 1380 * 1381 * Returns: the data if found, or %NULL 1382 * if no such data exists. 1383 */ 1384 public void* stealData(string key) 1385 { 1386 return g_object_steal_data(gObject, Str.toStringz(key)); 1387 } 1388 1389 /** 1390 * This function gets back user data pointers stored via 1391 * g_object_set_qdata() and removes the @data from object 1392 * without invoking its destroy() function (if any was 1393 * set). 1394 * Usually, calling this function is only required to update 1395 * user data pointers with a destroy notifier, for example: 1396 * |[<!-- language="C" --> 1397 * void 1398 * object_add_to_user_list (GObject *object, 1399 * const gchar *new_string) 1400 * { 1401 * // the quark, naming the object data 1402 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list"); 1403 * // retrieve the old string list 1404 * GList *list = g_object_steal_qdata (object, quark_string_list); 1405 * 1406 * // prepend new string 1407 * list = g_list_prepend (list, g_strdup (new_string)); 1408 * // this changed 'list', so we need to set it again 1409 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list); 1410 * } 1411 * static void 1412 * free_string_list (gpointer data) 1413 * { 1414 * GList *node, *list = data; 1415 * 1416 * for (node = list; node; node = node->next) 1417 * g_free (node->data); 1418 * g_list_free (list); 1419 * } 1420 * ]| 1421 * Using g_object_get_qdata() in the above example, instead of 1422 * g_object_steal_qdata() would have left the destroy function set, 1423 * and thus the partial string list would have been freed upon 1424 * g_object_set_qdata_full(). 1425 * 1426 * Params: 1427 * quark = A #GQuark, naming the user data pointer 1428 * 1429 * Returns: The user data pointer set, or %NULL 1430 */ 1431 public void* stealQdata(GQuark quark) 1432 { 1433 return g_object_steal_qdata(gObject, quark); 1434 } 1435 1436 /** 1437 * If @object is floating, sink it. Otherwise, do nothing. 1438 * 1439 * In other words, this function will convert a floating reference (if 1440 * present) into a full reference. 1441 * 1442 * Typically you want to use g_object_ref_sink() in order to 1443 * automatically do the correct thing with respect to floating or 1444 * non-floating references, but there is one specific scenario where 1445 * this function is helpful. 1446 * 1447 * The situation where this function is helpful is when creating an API 1448 * that allows the user to provide a callback function that returns a 1449 * GObject. We certainly want to allow the user the flexibility to 1450 * return a non-floating reference from this callback (for the case 1451 * where the object that is being returned already exists). 1452 * 1453 * At the same time, the API style of some popular GObject-based 1454 * libraries (such as Gtk) make it likely that for newly-created GObject 1455 * instances, the user can be saved some typing if they are allowed to 1456 * return a floating reference. 1457 * 1458 * Using this function on the return value of the user's callback allows 1459 * the user to do whichever is more convenient for them. The caller will 1460 * alway receives exactly one full reference to the value: either the 1461 * one that was returned in the first place, or a floating reference 1462 * that has been converted to a full reference. 1463 * 1464 * This function has an odd interaction when combined with 1465 * g_object_ref_sink() running at the same time in another thread on 1466 * the same #GObject instance. If g_object_ref_sink() runs first then 1467 * the result will be that the floating reference is converted to a hard 1468 * reference. If g_object_take_ref() runs first then the result will be 1469 * that the floating reference is converted to a hard reference and an 1470 * additional reference on top of that one is added. It is best to avoid 1471 * this situation. 1472 * 1473 * Returns: @object 1474 * 1475 * Since: 2.70 1476 */ 1477 public ObjectG takeRef() 1478 { 1479 auto __p = g_object_take_ref(gObject); 1480 1481 if(__p is null) 1482 { 1483 return null; 1484 } 1485 1486 return ObjectG.getDObject!(ObjectG)(cast(GObject*) __p, true); 1487 } 1488 1489 /** 1490 * Reverts the effect of a previous call to 1491 * g_object_freeze_notify(). The freeze count is decreased on @object 1492 * and when it reaches zero, queued "notify" signals are emitted. 1493 * 1494 * Duplicate notifications for each property are squashed so that at most one 1495 * #GObject::notify signal is emitted for each property, in the reverse order 1496 * in which they have been queued. 1497 * 1498 * It is an error to call this function when the freeze count is zero. 1499 */ 1500 public void thawNotify() 1501 { 1502 g_object_thaw_notify(gObject); 1503 } 1504 1505 /** 1506 * Decreases the reference count of @object. When its reference count 1507 * drops to 0, the object is finalized (i.e. its memory is freed). 1508 * 1509 * If the pointer to the #GObject may be reused in future (for example, if it is 1510 * an instance variable of another object), it is recommended to clear the 1511 * pointer to %NULL rather than retain a dangling pointer to a potentially 1512 * invalid #GObject instance. Use g_clear_object() for this. 1513 */ 1514 public void unref() 1515 { 1516 g_object_unref(gObject); 1517 } 1518 1519 /** 1520 * This function essentially limits the life time of the @closure to 1521 * the life time of the object. That is, when the object is finalized, 1522 * the @closure is invalidated by calling g_closure_invalidate() on 1523 * it, in order to prevent invocations of the closure with a finalized 1524 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are 1525 * added as marshal guards to the @closure, to ensure that an extra 1526 * reference count is held on @object during invocation of the 1527 * @closure. Usually, this function will be called on closures that 1528 * use this @object as closure data. 1529 * 1530 * Params: 1531 * closure = #GClosure to watch 1532 */ 1533 public void watchClosure(Closure closure) 1534 { 1535 g_object_watch_closure(gObject, (closure is null) ? null : closure.getClosureStruct()); 1536 } 1537 1538 /** 1539 * Adds a weak reference callback to an object. Weak references are 1540 * used for notification when an object is disposed. They are called 1541 * "weak references" because they allow you to safely hold a pointer 1542 * to an object without calling g_object_ref() (g_object_ref() adds a 1543 * strong reference, that is, forces the object to stay alive). 1544 * 1545 * Note that the weak references created by this method are not 1546 * thread-safe: they cannot safely be used in one thread if the 1547 * object's last g_object_unref() might happen in another thread. 1548 * Use #GWeakRef if thread-safety is required. 1549 * 1550 * Params: 1551 * notify = callback to invoke before the object is freed 1552 * data = extra data to pass to notify 1553 */ 1554 public void weakRef(GWeakNotify notify, void* data) 1555 { 1556 g_object_weak_ref(gObject, notify, data); 1557 } 1558 1559 /** 1560 * Removes a weak reference callback to an object. 1561 * 1562 * Params: 1563 * notify = callback to search for 1564 * data = data to search for 1565 */ 1566 public void weakUnref(GWeakNotify notify, void* data) 1567 { 1568 g_object_weak_unref(gObject, notify, data); 1569 } 1570 1571 /** 1572 * Clears a reference to a #GObject. 1573 * 1574 * @object_ptr must not be %NULL. 1575 * 1576 * If the reference is %NULL then this function does nothing. 1577 * Otherwise, the reference count of the object is decreased and the 1578 * pointer is set to %NULL. 1579 * 1580 * A macro is also included that allows this function to be used without 1581 * pointer casts. 1582 * 1583 * Params: 1584 * objectPtr = a pointer to a #GObject reference 1585 * 1586 * Since: 2.28 1587 */ 1588 public static void clearObject(ref ObjectG objectPtr) 1589 { 1590 GObject* outobjectPtr = objectPtr.getObjectGStruct(); 1591 1592 g_clear_object(&outobjectPtr); 1593 1594 objectPtr = ObjectG.getDObject!(ObjectG)(outobjectPtr); 1595 } 1596 }